package com.houfukude.Utils;

import java.io.File;

import com.houfukude.Activity.MainActivity;
import com.houfukude.Activity.NoteListActivity;
import com.houfukude.xtestmgr.R;

import Utils.DateDilogBuilder;
import com.houfukude.Utils.FTP_Server;
import Utils.File_Utils;
import Utils.V;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

public class ActionBar_Utils {
	private static String DEBUG_TAG = "ActionBar_Utils";
	private static FTP_Server ftp_server = null;

	public static String Image_Name = null;
	/**
	 * 下拉菜单适配器
	 */
	private static SpinnerAdapter adapter;

	/**
	 * ActionBar没有下拉的时候调用的方法 用于设置标题
	 * 
	 * @author houfukude
	 */
	public static void restoreActionBar(CharSequence mTitle, ActionBar actionBar) {
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	/**
	 * 传入适配器和监听器创建下拉菜单并通过回调进行监听
	 * 
	 * @param adapter
	 *            传入自定义的SpinnerAdapter
	 * @param navListenser
	 *            传入自定义OnNavigationListener
	 * @param actionBar
	 *            可以通过在mainActivity中的
	 *            {@link android.support.v7.app.ActionBarActivity#getSupportActionBar
	 *            getSupportActionBar()}获得
	 */
	public static void restoreActionBar(SpinnerAdapter adapter,
			OnNavigationListener navListenser, ActionBar actionBar) {
		actionBar.setTitle(null);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		actionBar.setListNavigationCallbacks(adapter, navListenser);

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_add_image}
	 * 来进行的操作<br>
	 * <br>
	 * 方法 {@link ActionBar_Utils#AddVideoItemSelected(MenuItem, Activity)
	 * AddVideoItemSelected}与
	 * {@link ActionBar_Utils#AddAudioItemSelected(MenuItem, Activity)
	 * AddAudioItemSelected } <br>
	 * 两个的方式与之基本相同基本相同 <br>
	 * 不同的是 Video 和Audio 不需要进行设置路径转Uri
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void AddImageItemSelected(MenuItem item, Activity activity) {
		Log.d(DEBUG_TAG, "action_add_image ItemSelected");

		// 创建一个Intent.ACTION_PICK，等待绑定相应的Intent
		Intent image_chooser = Intent.createChooser(new Intent(
				Intent.ACTION_PICK), activity.getText(R.string.image_chooser));

		// 创建一个调用系统相机的Intent
		Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

		// 设置系统相机拍摄照片完成后图片文件的存放地址
		// 并把存放地址封装成Uri
		Image_Name = File_Utils.get_Image_Name();
		Uri camerauri = Uri.fromFile(new File(V.Image_Directory, Image_Name));
		Log.i(DEBUG_TAG, "image name:" + Image_Name);

		// 把封装的Uri传入系统相机Intent
		camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, camerauri);

		// 创建一个Intent.ACTION_GET_CONTENT 并通过设置MIMETYPE设置成为图片选择模式
		Intent image_select_intent = new Intent(Intent.ACTION_GET_CONTENT);
		image_select_intent.setType("image/*");

		// 把刚才建立的两个Intent传入最开始创建的Intent.ACTION_PICK
		image_chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {
				camera_intent, image_select_intent });

		// 启动最开始创建的Intent.ACTION_PICK，并获得返回的Uri
		activity.startActivityForResult(image_chooser, V.id.ADD_IMAGE);
	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_add_video}
	 * 来进行的操作<br>
	 * <br>
	 * 过程与方式与{@link ActionBar_Utils#AddImageItemSelected(MenuItem, Activity)
	 * AddImageItemSelected}基本相似
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void AddVideoItemSelected(MenuItem item, Activity activity) {
		Log.d(DEBUG_TAG, "action_add_video ItemSelected");

		Intent video_chooser = Intent.createChooser(new Intent(
				Intent.ACTION_PICK), activity.getText(R.string.video_chooser));

		Intent video_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

		Intent video_select_intent = new Intent(Intent.ACTION_GET_CONTENT);
		video_select_intent.setType("video/*");

		video_chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {
				video_intent, video_select_intent });

		activity.startActivityForResult(video_chooser, V.id.ADD_VIDEO);

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当 {@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_add_audio}
	 * 来进行的操作<br>
	 * <br>
	 * 过程与方式与{@link ActionBar_Utils#AddImageItemSelected(MenuItem, Activity)
	 * AddImageItemSelected}基本相似
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void AddAudioItemSelected(MenuItem item, Activity activity) {
		Log.d(DEBUG_TAG, "action_add_audio ItemSelected");
		Intent audio_chooser = Intent.createChooser(new Intent(
				Intent.ACTION_PICK), activity.getText(R.string.audio_chooser));
		Intent audio_intent = new Intent(
				MediaStore.Audio.Media.RECORD_SOUND_ACTION);
		Intent audio_select_intent = new Intent(Intent.ACTION_GET_CONTENT);
		audio_select_intent.setType("audio/*");

		audio_chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {
				audio_intent, audio_select_intent });
		activity.startActivityForResult(audio_chooser, V.id.ADD_AUDIO);

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_add_note}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void AddNoteItemSelected(MenuItem item, Activity activity) {
		Intent intent = new Intent("com.houfukude.Activity.NewNoteActivity");
		activity.startActivityForResult(intent, V.id.ADD_NOTE);
	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_openwifi}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void OpenwifiItemSelected(MenuItem item, Activity activity) {
		activity.startActivity(new Intent(
				android.provider.Settings.ACTION_WIFI_SETTINGS));

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_openap}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void OpenapItemSelected(MenuItem item, Activity activity) {
		Intent tetherSettings = new Intent();
		tetherSettings.setClassName("com.android.settings",
				"com.android.settings.TetherSettings");
		activity.startActivity(tetherSettings);
	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_refresh}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void RefreshItemSelected(MenuItem item,
			final Activity activity) {
		Toast.makeText(activity, "正在刷新...", Toast.LENGTH_SHORT).show();
		
		((MainActivity) activity).ftp_layout.removeAllViews();
		new Thread(new Runnable() {
			@Override
			public void run() {
				((MainActivity) activity).ftp_Client.Initalize(null);
				((MainActivity) activity).list = ((MainActivity) activity).ftp_Client
						.getFiles();
				((MainActivity) activity).handler
						.sendEmptyMessage(V.what.FTP_Initalize);
			}
		}).start();
		// try {
		// WebView webView = (WebView) activity.findViewById(R.id.webView);
		// webView.reload();
		// } catch (Exception e) {
		// Log.e(DEBUG_TAG, "!!!no webview found");
		// }
	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_server}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void ServerItemSelected(MenuItem item, Activity activity) {
		if (ftp_server == null) {
			ftp_server = new FTP_Server();
			if (ftp_server.start()) {
				item.setTitle("关闭发送");
				Toast.makeText(activity, "发送服务启动成功！", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(activity, "发送服务启动失败！", Toast.LENGTH_LONG).show();
			}

		} else {
			ftp_server.stop();
			ftp_server = null;
			item.setTitle("启动发送");
			Toast.makeText(activity, "发送服务已关闭！", Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_search}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void SearchNoteItemSelected(MenuItem item, Activity activity) {
		Toast.makeText(activity, "SearchNoteItemSelected", Toast.LENGTH_SHORT)
				.show();

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_filter}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void NoteFilterItemSelected(MenuItem item,
			final Activity activity) {

		final DateDilogBuilder dateTimeBuilder = new DateDilogBuilder();

		OnClickListener positiveClickListener = new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String timepick = dateTimeBuilder.getString();
				Toast.makeText(activity, "Time:" + timepick, Toast.LENGTH_SHORT)
						.show();
			}
		};

		dateTimeBuilder.Instance(activity, positiveClickListener, null);

	}

	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_note_save}
	 * 来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void NoteSaveItemSelected(MenuItem item, Activity activity) {
		Toast.makeText(activity, "NoteSaveItemSelected", Toast.LENGTH_SHORT)
				.show();

	}


	/**
	 * 处理Activity的{@link Activity#onOptionsItemSelected(MenuItem)
	 * onOptionsItemSelected()}<br>
	 * 当{@link MenuItem#getItemId() getItemId()}等于{@link R.id#action_test}来进行的操作
	 * 
	 * @param item
	 * @param activity
	 * @author houfukude
	 */
	public static void TestItemSelected(MenuItem item, Activity activity) {
		Toast.makeText(activity, "TestItemSelected", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent("com.houfukude.Activity.testActivity");
		activity.startActivityForResult(intent, 0);

	}

	/**
	 * 这是俺拿来测试实验用的 推荐使用
	 * {@link com.houfukude.Utils.ActionBar_Utils#restoreActionBar(SpinnerAdapter, OnNavigationListener, ActionBar)
	 * restoreActionBar}
	 * 
	 * @author houfukude
	 */
	@Deprecated
	public static void restoreActionBar(final Activity activity,
			ActionBar actionBar) {
		/**
		 * 判断API level>14的时候，对UI进行补偿设置
		 */
		if (V.isICS) {
			/**
			 * 黑底白字
			 */
			adapter = ArrayAdapter.createFromResource(activity,
					R.array.note_type, R.layout.my_spinner_dropdown_item);
		} else {
			/**
			 * 白底黑字
			 */
			adapter = ArrayAdapter.createFromResource(activity,
					R.array.note_type,
					R.layout.support_simple_spinner_dropdown_item);
		}

		OnNavigationListener navListenser = new OnNavigationListener() {

			@Override
			public boolean onNavigationItemSelected(int itemPosition, long id) {
				Toast.makeText(activity,
						adapter.getItem(itemPosition).toString(),
						Toast.LENGTH_SHORT).show();
				return false;
			}
		};

		restoreActionBar(adapter, navListenser, actionBar);

	}

}
